global datapath "~/DataFolder"
global Analyses "~/Results"

***************************************************** Table 13: Negative Lending
use "$datapath/RegData.dta", clear

global controls riskiness male avg_minority branch distance numsbl lretain size loan deposit p_ci p_re roa liquid

gen str5 county_fips = string(fips,"%05.0f")

*merge conventional loan
rename raw_num raw_num_con
rename raw_volume raw_volume_con
rename total_applications total_applications_con

merge 1:1 rssdhcr year fips using "$datapath/RegData_gov.dta", keepusing(raw_num raw_volume total_applications )

rename raw_num raw_num_gov
rename raw_volume raw_volume_gov
rename total_applications total_applications_gov

drop if _merge ==2 
drop _merge

replace raw_num_gov = 0 if missing(raw_num_gov)
replace raw_volume_gov = 0 if missing(raw_volume_gov)
replace total_applications_gov = 0 if missing(total_applications_gov)
replace raw_num_con = 0 if missing(raw_num_con)
replace raw_volume_con = 0 if missing(raw_volume_con)
replace total_applications_con = 0 if missing(total_applications_con)

gen p_num_con = raw_num_con / (raw_num_gov+raw_num_con)
gen p_vol_con = raw_volume_con / (raw_volume_gov+raw_volume_con )

merge 1:1 rssdhcr year fips using "$datapath/Bank_County_IndustryShock.dta"
drop if _merge==2
drop _merge

replace aggIndNegShock_med_30= 0 if missing(aggIndNegShock_med_30)

winsor2 d60 d90 foreclosure credit ltv dti itrate avgloan_income avgmale avgminority p_num_con p_vol_con, cut(2.5 97.5) by(year) replace

cd "$Analyses"

capture drop spec_inddistress
gen spec_inddistress = aggIndNegShock_med_30

reghdfe ltv spec_above5pctemp spec_inddistress $controls , absorb(county_year bank_state bank_year) cluster(fips)
outreg2 using Table13.xls, replace label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(County*Year FE, Yes, Bank*State FE, Yes, Bank*Year FE, Yes)

reghdfe itrate spec_above5pctemp spec_inddistress $controls , absorb(county_year bank_state bank_year) cluster(fips)
outreg2 using Table13.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(County*Year FE, Yes, Bank*State FE, Yes, Bank*Year FE, Yes)

reghdfe p_num_con spec_above5pctemp spec_inddistress $controls , absorb(county_year bank_state bank_year) cluster(fips)
outreg2 using Table13.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(County*Year FE, Yes, Bank*State FE, Yes, Bank*Year FE, Yes)

reghdfe p_vol_con spec_above5pctemp spec_inddistress $controls , absorb(county_year bank_state bank_year) cluster(fips)
outreg2 using Table13.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(County*Year FE, Yes, Bank*State FE, Yes, Bank*Year FE, Yes)
